package com.example.imitation_webchat_demo.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.controller.MessageHandler;
import com.example.imitation_webchat_demo.entity.NoReadMesInfo;
import com.example.imitation_webchat_demo.fragment.AddressListFragment;
import com.example.imitation_webchat_demo.fragment.MessageFragment;
import com.example.imitation_webchat_demo.fragment.MineFragment;
import com.example.imitation_webchat_demo.fragment.NewlyConditionFragment;
import com.example.imitation_webchat_demo.model.BadgeView;
import com.example.imitation_webchat_demo.model.MyHandler;
import com.example.imitation_webchat_demo.util.AppUtils;
import com.example.imitation_webchat_demo.util.ApplicationUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

// 仿QQ主界面
public class MainActivity extends Activity {
	private FragmentManager fm;
	private FragmentTransaction transaction;

	@ViewInject(R.id.main_message)
	private RadioButton messageBtn;

	@ViewInject(R.id.main_mine)
	private RadioButton mineBtn;

	@ViewInject(R.id.main_newly_condition)
	private RadioButton newlyConditionBtn;

	@ViewInject(R.id.main_address_list)
	private RadioButton addressListBtn;

	@ViewInject(R.id.main_radiogroup)
	private RadioGroup radioGroup;

	private BadgeView messageBadgeView;
	private BadgeView mineBadgeView;
	private BadgeView conditionBadgeView;
	private BadgeView addressBadgeView;

	private MessageFragment messageFragment;
	private MineFragment mineFragment;
	private NewlyConditionFragment newlyConditionFragment;
	private AddressListFragment addressListFragment;

	private boolean isExit;
	private MyHandler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题模式
		setContentView(R.layout.activity_main);

		ViewUtils.inject(this);
		initComponent(); // 初始化控件

		ApplicationUtils.getInstance().addActivity(this);
	}

	// 初始化控件
	public void initComponent() {
		messageBadgeView = new BadgeView(this, messageBtn);
		mineBadgeView = new BadgeView(this, mineBtn);
		conditionBadgeView = new BadgeView(this, newlyConditionBtn);
		addressBadgeView = new BadgeView(this, addressListBtn);
		messageBadgeView.setIntegerText(8);
		mineBadgeView.setIntegerText(0);
		conditionBadgeView.setIntegerText(0);
		addressBadgeView.setIntegerText(0);

		fm = this.getFragmentManager();
		messageFragment = new MessageFragment();
		mineFragment = new MineFragment();
		newlyConditionFragment = new NewlyConditionFragment();
		addressListFragment = new AddressListFragment();
		transaction = fm.beginTransaction();
		transaction.add(R.id.main_content, mineFragment);
		transaction.add(R.id.main_content, newlyConditionFragment);
		transaction.add(R.id.main_content, addressListFragment);
		transaction.add(R.id.main_content, messageFragment);
		transaction.commit();

		isExit = false;
		handler = new MyHandler(this) {
			@Override
			public void handleMessage(Message msg) {
				isExit = false;
				super.handleMessage(msg);
			}
		};
		
		messageFragment.setMessageHandler(new MessageHandler(){
			@Override
			public void onReceive(NoReadMesInfo info) {
				String type = info.getType();
				int number = info.getNumber();
				if(type.equals("no read message")){
					messageBadgeView.setIntegerText(number);
				}
			}
		});
	}

	// 隐藏所有的fragment
	public void hideFragments() {
		transaction.hide(messageFragment);
		transaction.hide(mineFragment);
		transaction.hide(newlyConditionFragment);
		transaction.hide(addressListFragment);
	}

	// 点击消息按钮时的响应
	@OnClick(R.id.main_message)
	public void showChatRoom(View view) {
		transaction = fm.beginTransaction();
		hideFragments();
		transaction.show(messageFragment);
		transaction.commit();

		showFragment(messageBtn);
	}

	// 点击我的按钮时的响应
	@OnClick(R.id.main_mine)
	public void showContactList(View view) {
		transaction = fm.beginTransaction();
		hideFragments();
		transaction.show(mineFragment);
		transaction.commit();

		showFragment(mineBtn);
	}

	// 点击动态按钮时的响应
	@OnClick(R.id.main_newly_condition)
	public void showNewlyCondition(View view) {
		transaction = fm.beginTransaction();
		hideFragments();
		transaction.show(newlyConditionFragment);
		transaction.commit();

		showFragment(newlyConditionBtn);
	}

	// 点击通讯录按钮时的响应
	@OnClick(R.id.main_address_list)
	public void showAddressList(View view) {
		transaction = fm.beginTransaction();
		hideFragments();
		transaction.show(addressListFragment);
		transaction.commit();

		showFragment(addressListBtn);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		ApplicationUtils.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if (!isExit) {
			isExit = true;
			handler.sendEmptyMessageDelayed(0, 2000);
			AppUtils.showToast(this, "请再按一次退出程序");
		} else {
//			super.onBackPressed();
			
			Log.v("TAG", "adsfadsfsd");
			
			ApplicationUtils.getInstance().exit();
		}
	}

	private void showFragment(RadioButton radioButton) {
		View view = null;
		View childView = null;
		for (int i = 0; i < radioGroup.getChildCount(); i++) {
			view = radioGroup.getChildAt(i);
			if (view instanceof FrameLayout) {
				for (int j = 0; j < ((FrameLayout) view).getChildCount(); j++) {
					childView = ((FrameLayout) view).getChildAt(j);
					if(childView instanceof RadioButton){
						((RadioButton)childView).setChecked(false);
					}
				}
			}
			if (view instanceof RadioButton) {
				((RadioButton) view).setChecked(false);
			}
		}
		radioButton.setChecked(true);
	}
}